/**
 * FtpParamsAction.java
 * Created at 2017-8-7
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.rms.basic.model.TtClientParams;
import com.broadtext.rms.basic.service.IClientParamsService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: FtpParamsAction</p>
 * <p>Description: Ftp参数配置控制层</p>
 * <p>Author: xieshuang</p>
 * <p>Date: 2017-8-7</p>
 */
@Controller
@RequestMapping("params/client")
public class ClientParamsAction {
	/**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(ClientParamsAction.class);
    /**
     * 注入service
     */
    @Autowired
    @Qualifier("clientParamsService")
    private IClientParamsService clientParamsService;
    
    /**
     * <p>
     * Description: FTP参数配置分页查询
     * </p>
     * 
     * @param request 请求对象
     * @param response 返回对象
     */
    @RequestMapping(value = "/getClientParamsList.mvc")
    @ResponseBody
    public Object getFtpParamsList(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> dataMap;
        dataMap = new HashMap<String, Object>();
        try {
            PageBounds pageBounds;
            pageBounds = new PageBounds();
            Map<String, Object> map;
            String aoData;
            aoData = request.getParameter("aoData");
            JSONArray array;
            array = JSON.parseArray(aoData);
            map = new HashMap<String, Object>();
            if (array != null) {
                Iterator<?> it;
                it = array.iterator();
                while (it.hasNext()) {
                    JSONObject obj;
                    obj = (JSONObject) it.next();
                    if (obj.get("value") != null && !"".equals(obj.get("value") + "")) {
                        map.put(obj.get("name") + "", obj.get("value"));
                    }
                }
            }
            pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
            pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
            EntityPageBean<TtClientParams> pageBean = this.clientParamsService.findEntityPageByQuery(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());

        } catch (ServiceException e) {
            logger.error("电池pack生产厂分页查询出错：", e);
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>
     * Description: 新增或更新FTP参数
     * </p>
     * 
     * @param t 对象
     * @param request 请求
     * @param response 响应
     */
    @RequestMapping(value = "/saveOrUpdate.mvc")
    @ResponseBody
    public Object saveOrUpdate(TtClientParams t, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> dataMap;
        dataMap = new HashMap<String, Object>();
        try {
            //当前登录人
            String currentUser;
            currentUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
            this.clientParamsService.saveOrUpdate(t,currentUser);
        } catch (ServiceException e) {
            dataMap.put("msg", "FAILURE");
            logger.error("保存出错：", e);
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: 根据id获取ftp参数配置记录</p>
     * @param request 请求
     * @param pkId ID
     * @return ftp参数配置记录
     */
    @RequestMapping(value = "getFTPParamsById.mvc")
    @ResponseBody
    public TtClientParams getFTPParamsById(HttpServletRequest request, String pkId) {
    	TtClientParams model;
        model = new TtClientParams();
        try {
            model = this.clientParamsService.findEntityById(pkId);
        } catch (ServiceException e) {
        	logger.error("查询出错：", e);
            e.printStackTrace();
        }
        return model;
    }
    
    /**
     * 
     * <p>Description:删除</p>
     * @param request 请求
     * @param response 响应
     */
    @RequestMapping(value = "/delete.mvc")
    @ResponseBody
    public Map<String, Object> delete(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        String ids;
        ids = request.getParameter("pkId");
        String[] idArray;
        idArray = ids.split(",");
        String sysUser;
        sysUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        try {
        	this.clientParamsService.deleteByIds(idArray, sysUser);
            map.put("msg", "success");
        } catch (ServiceException e) {
        	logger.error("删除出错：", e);
        	map.put("msg", "failure");
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: ftp下拉框异步加载</p>
     * @param res 请求
     * @return 结果集
     */
    @RequestMapping(value="/getFtpParamsOptions.mvc")
    @ResponseBody
    public Object getFtpParamsOptions(HttpServletRequest res){
    	
    	Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
			map.put("data", this.clientParamsService.getFtpParamsOptions());
		} catch (DaoException e) {
			logger.error("查询出错：", e);
			e.printStackTrace();
		}
        return map;
    }
    /**
     * 
     * <p>Description: client下拉框异步加载</p>
     * @param res 请求
     * @return 结果集
     */
    @RequestMapping(value="/getClientOptions.mvc")
    @ResponseBody
    public Object getClientOptions(HttpServletRequest res){
    	
    	Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
			map.put("data", this.clientParamsService.getClientOptions());
		} catch (DaoException e) {
			logger.error("查询出错：", e);
			e.printStackTrace();
		}
        return map;
    }
    
}
